package com.team2.outpatient_sys.web.pharmacyControllers;

import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.pojo.Dispense;
import com.team2.outpatient_sys.pojo.Medicine;
import com.team2.outpatient_sys.pojo.Prescription;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.service.DispenseService;
import com.team2.outpatient_sys.service.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dispense")
public class DispenseController {

    @Autowired
    private DispenseService dispenseService;

    @Autowired
    private PrescriptionService prescriptionService;

    @PostMapping("/add")
    public Result add(@RequestBody Dispense dispense) {
        Result result = new Result();
        if (dispenseService.add(dispense)) {
            result.setCode(200);
            result.setMsg("增加成功");
        } else {
            result.setCode(500);
            result.setMsg("增加失败");
        }
        return result;
    }


    @PutMapping("/update")
    public Result update(@RequestBody Dispense dispense) {
        Result result = new Result();
        if (dispenseService.update(dispense)) {
            result.setCode(200);
            result.setMsg("修改成功");
        } else {
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }

    @GetMapping
    public Result findAll() {
        List<Dispense> list = dispenseService.findAll();
        Result result = new Result();
        if (list != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Dispense dispense = dispenseService.findById(id);
        Result result = new Result();
        if (dispense != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(dispense);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = dispenseService.delete(id);
        Result result = new Result();
        if (flag) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }





    @GetMapping("/findDispense/{prescriptionId}")
    public Result findDispense(@PathVariable Integer prescriptionId){
        List<Medicine> list=dispenseService.findDispense(prescriptionId);
        Result result=new Result();
        if(list !=null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }else{
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public Result findByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        List<Prescription> list = prescriptionService.findByPage(currentPage,pageSize);
        Result result = new Result();
        if (list != null) {
            PageInfo<Prescription> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/findAllPrescription")
    public Result findAllPrescription(){
        List<Prescription> list=prescriptionService.findAllPrescription();
        Result result = new Result();
        if (list != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        } else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
}
